package com.example.zbh.lessona.eventbus;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.Button;

import com.example.zbh.lessona.R;

import de.greenrobot.event.EventBus;

/**
 * Created by zbh on 15/12/2.
 */
public class BusActivity extends FragmentActivity implements View.OnClickListener {
    private FragmentManager manager;
    private Button button;
    private PlayService.MyBinder myBinder;
    private ServiceConnection connection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            if (service != null){
                myBinder = (PlayService.MyBinder) service;
            }

        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bus);
        initView();
    }

    private void initView() {
        manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.replace(R.id.left, new LeftFragment());
        transaction.replace(R.id.right, new RightFragment());
        transaction.commit();
        button = (Button) findViewById(R.id.button);
        button.setOnClickListener(this);
        Intent intent = new Intent();
        intent.setClass(this, PlayService.class);
        bindService(intent, connection, BIND_AUTO_CREATE);



    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbindService(connection);


    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.button:
                myBinder.sendMsg();
                break;
        }
    }
}
